package com.briup.sxau.web.controller;

import com.briup.sxau.util.Result;
import com.briup.sxau.util.SxauException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "文件管理相关接口")
@RequestMapping("file")
public class FileController {
    @PostMapping("upload")
    @ApiOperation("图片上传,文件格式为jpg/pang")
    private Result upload(@RequestBody MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] strings = originalFilename.split("[.]");
//        校验文件名称
        String fileType = strings[strings.length-1];
        if (fileType.toLowerCase().equals("png")||fileType.toLowerCase().equals("jpg")){
//            文件保存  选择在本地要保存的路径  /usr/local/var/www
            File destFile = new File("E:\\大二下学期实训\\nginx-1.25.0\\html\\img\\" + UUID.randomUUID().toString() + "." + fileType);
            file.transferTo(destFile);
            System.out.println("图片已经存放到目标文件夹，请检查");
            return Result.success();
        }else {
            throw new SxauException("文件格式不支持，只支持PNG、以及JPG两种格式的文件");
        }

    }



}

